package com.ai.module.order.config;

import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.security.PrivateKey;
import java.security.cert.X509Certificate;

/**
 *  微信相关配置类
 */
@Data
@Slf4j
@Configuration
public class WeChatConfig {

    /**
     * 预支付订单url
     */
    private String preOrderUrl = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi";

    /**
     * 商户号id
     */
    @Value("${pay.wechat.mchId}")
    private String mchId;



    /**
     * 微信appid
     */
    @Value("${pay.wechat.appId}")
    private String appid;

    /**
     * 微信APIV3支付证书
     */
    @Value("${pay.wechat.certUrl}")
    private String certUrl;

    /**
     * 商家密钥
     */
    @Value("${pay.wechat.privateKeyPath}")
    private String privateKeyUrl;

    /**
     * 商家密钥
     */
    @Value("${pay.wechat.mchSerialNo}")
    private String mchSerialNo;

    /**
     * appV3 key
     */
    @Value("${pay.wechat.apiV3Key}")
    private String appV3Key;

    /**
     * 获取微信支付api V3支付证书
     * @return
     */
    public X509Certificate getCertificate() {
        try {
            return  PemUtil.loadCertificate(new FileInputStream(certUrl));
        } catch (FileNotFoundException e) {
            log.error("加载微信支付APIv3支付证书失败{}", e.getMessage(), e);
        }
        return null;
    }

    public PrivateKey getPrivateKey() {
        try {
            return  PemUtil.loadPrivateKey(new FileInputStream(privateKeyUrl));
        } catch (FileNotFoundException e) {
            log.error("加载微信支付APIv3支付证书失败{}", e.getMessage(), e);
        }
        return null;
    }
}
